package GUI;

import game.Item;
import game.ItemContainer;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ContainerTransactionPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = -680480886543462317L;
	
	private ItemContainerView c1 = null, c2 = null;
	private SkinnedButton transL = null, transR = null,
		selA1 = null, selN1 = null, selA2 = null, selN2 = null, btnClose = null;
	private JFrame contFrame = null;
	private JLabel leftTitle = null;
	private JLabel rightTitle = null;
	
	public static ContainerTransactionPanel showInFrame(Container parent, ItemContainer cL, String nL, ItemContainer cR, String nR)
	{
		ContainerTransactionPanel ctp = new ContainerTransactionPanel();
		ctp.setLeftContainer(cL, nL);
		ctp.setRightContainer(cR, nR);
		
		ctp.contFrame = new JFrame("Transaction");
		ctp.contFrame.setUndecorated(true);
		//ctp.contFrame.setLayout(null);
		ctp.contFrame.add(ctp);
		ctp.contFrame.pack();

		if(parent != null)
		{
			System.out.println("Haz parent");
			Rectangle rect = new Rectangle(parent.getLocationOnScreen(), parent.getSize());
			ctp.contFrame.setLocation(rect.x + (rect.width - ctp.contFrame.getWidth()) / 2,
					rect.y + (rect.height - ctp.contFrame.getHeight()) / 2);
		}
		
		ctp.contFrame.setVisible(true);
		
		return ctp;
	}
	
	public ContainerTransactionPanel()
	{
		super();
		
		this.setMinimumSize(new Dimension(800, 470));
		this.setPreferredSize(new Dimension(800, 470));
		this.setMaximumSize(new Dimension(800, 470));
		
		this.setLayout(null);
		this.setBackground(new Color(48, 48, 48));
		
		c1 = new ItemContainerView(null);
		c2 = new ItemContainerView(null);
		
		this.add(c1);
		c1.setSize(350, 405);
		c1.setLocation(0, 30);
		
		this.add(c2);
		c2.setSize(350, 405);
		c2.setLocation(450, 30);
		
		transL = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "<-");
		transR = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "->");
		btnClose = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "X");
		
		selA1 = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "Select All");
		selN1 = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "Select None");
		selA2 = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "Select All");
		selN2 = new SkinnedButton(ButtonSkin.getButtonByTag("Button1"), "Select None");
		
		this.add(selA1);
		this.add(selN1);
		this.add(selA2);
		this.add(selN2);
		
		selA1.setSize(175, 30);
		selN1.setSize(175, 30);
		selA2.setSize(175, 30);
		selN2.setSize(175, 30);
		
		selA1.setLocation(0, 440);
		selN1.setLocation(175, 440);
		selA2.setLocation(450, 440);
		selN2.setLocation(625, 440);
		
		this.add(transL);
		this.add(transR);
		this.add(btnClose);
		transL.setLocation(350, 130);
		transR.setLocation(350, 200);
		btnClose.setLocation(350, 360);
		transL.setSize(100, 50);
		transR.setSize(100, 50);
		btnClose.setSize(100, 50);
		
		leftTitle = new JLabel("Left", JLabel.CENTER);
		rightTitle = new JLabel("Right", JLabel.CENTER);
		
		leftTitle.setForeground(Color.white);
		rightTitle.setForeground(Color.white);
		
		this.add(leftTitle);
		this.add(rightTitle);
		leftTitle.setLocation(0, 0);
		leftTitle.setSize(350, 30);
		rightTitle.setLocation(450, 0);
		rightTitle.setSize(350, 30);
		
		selA1.addActionListener(this);
		selN1.addActionListener(this);
		selA2.addActionListener(this);
		selN2.addActionListener(this);
		
		transL.addActionListener(this);
		transR.addActionListener(this);
		btnClose.addActionListener(this);
	}
	
	public void setLeftContainer(ItemContainer cont, String name)
	{
		c1.setItemContainer(cont);
		leftTitle.setText(name);
	}
	
	public void setRightContainer(ItemContainer cont, String name)
	{
		c2.setItemContainer(cont);
		rightTitle.setText(name);
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource() == selA1)
		{
			c1.selectAll();
		}
		else if(arg0.getSource() == selN1)
		{
			c1.selectNone();
		}
		else if(arg0.getSource() == selA2)
		{
			c2.selectAll();
		}
		else if(arg0.getSource() == selN2)
		{
			c2.selectNone();
		}
		else if(arg0.getSource() == transL)
		{
			transfer(c2, c1);
		}
		else if(arg0.getSource() == transR)
		{
			transfer(c1, c2);
		}
		else if(arg0.getSource() == btnClose)
		{
			if(contFrame != null)
			{
				contFrame.setVisible(false);
				contFrame.dispose();
			}
			else
			{
				this.setVisible(false);
			}
		}
	}
	
	private void transfer(ItemContainerView v1, ItemContainerView v2)
	{
		if(v1.getItemContainer() == null || v2.getItemContainer() == null) return;
		
		Vector<Item> items = v1.getSelectedItems();
		int w = 0;
		for(Item i : items)
			w += i.getWeight();
		
		if(v2.getItemContainer().getWeight() + w > v2.getItemContainer().getMaxWeight())
		{
			System.out.println("Pretesko, return");
			return;
		}
		
		if(v2.getItemContainer().getItemCount() + items.size() > 
			v2.getItemContainer().getMaxItemCount())
		{
			System.out.println("Previse, return");
			return;
		}
		
		for(Item i : items)
		{
			v1.getItemContainer().Remove(i);
			v2.getItemContainer().Add(i);
		}
		v1.refreshLists();
		v2.refreshLists();
		v1.repaint();
		v2.selectItems(items);
	}
}
